package com.sari.common.persistence;

import com.sari.common.utils.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.annotate.JsonIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by HiZhengfu on 2014/8/13.
 */
public class GridPage<T> {
    //当前页码
    private int pageNo;
    //每页数量
    private int pageSize;
    //总页数
    private long totalCount;

    //排序字段
    private String softName;
    //排列顺序
    private String orderBy = "asc"; // 标准查询有效， 实例： updatedate desc, name asc

    //数据体
    private List<T> items = new ArrayList<T>();

    public GridPage() {
        this.pageSize = -1;
    }

    /**
     * 分页是否有效
     *
     * @return this.pageSize==-1
     */
    @JsonIgnore
    public boolean isDisabled() {
        return this.pageSize == -1;
    }

    @JsonIgnore
    public int getFirstResult() {
        int firstRecord = (pageNo - 1) * getPageSize();
        if (firstRecord >= getTotalCount()) {
            firstRecord = 0;
        }
        return firstRecord;
    }

    /**
     * 构造方法
     *
     * @param response 用于设置 Cookie，记住页码
     */
    public GridPage(HttpServletRequest request, HttpServletResponse response) {
        this(request, response, -2);
    }

    public GridPage(HttpServletRequest request, HttpServletResponse response, int defaultPageSize) {
        // 设置页码参数,自动记忆当前页码
        String pageNo = request.getParameter("pageNo");
        if (StringUtils.isBlank(pageNo)) {
            pageNo = CookieUtils.getCookie(request, "pageNo");
        }
        if (StringUtils.isNumeric(pageNo)) {
            CookieUtils.setCookie(response, "pageNo", pageNo);
            this.setPageNo(Integer.parseInt(pageNo));
        }
        // 设置页面大小参数,自动记忆分页数量
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isBlank(pageSize)) {
            pageSize = CookieUtils.getCookie(request, "pageSize");
        }

        if (StringUtils.isNumeric(pageSize)) {
            CookieUtils.setCookie(response, "pageSize", pageSize);
            this.setPageSize(Integer.parseInt(pageSize));
        } else if (defaultPageSize != -2) {
            this.setPageSize(defaultPageSize);
        }
        // 设置排序参数
        String orderBy = request.getParameter("orderBy");
        if (StringUtils.isNotBlank(orderBy)) {
            this.setOrderBy(orderBy);
        }

        // 设置排序字段
        String sortName = request.getParameter("sortName");
        if (StringUtils.isNotBlank(sortName)) {
            this.setSoftName(sortName);
        }
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    public String getSoftName() {
        return softName;
    }

    public void setSoftName(String softName) {
        this.softName = softName;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }
}
